<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1600"  destroy-on-close @closed="$emit('closed')" align-center >
        <el-container>
            <el-header>
                <div class="left-panel">
                    <el-button type="primary" @click="back" v-if="type">确定</el-button>
                </div>
                <div class="right-panel">
                    <div class="right-panel-search">
                        <el-input v-model="search.purchase_code" placeholder="订单编号" clearable @keyup.enter="upsearch"></el-input>
                        <!-- <el-input v-model="search.purchase_code" placeholder="采购编号" clearable @keyup.enter="upsearch"></el-input> -->
                        <el-select v-model="search.input_type" placeholder="订单类型" @change="upsearch">
                            <el-option label="采购单" :value="1"></el-option>
                            <el-option label="染色单" :value="2"></el-option>
                        </el-select>
                        <!-- <el-select v-model="search.apply_id" placeholder="供应商" filterable clearable @change="applyChange(form.apply_id)">
                            <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                        </el-select> -->
                        <el-date-picker v-model="search.date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                        <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                    </div>
                </div>
            </el-header>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="apiObj" row-key="id" :params="query" height="700" @selection-change="selectionChange">
                    <el-table-column type="selection" width="50" align="center"></el-table-column>
                    <el-table-column label="入库类型" prop="purchase_code" width="180">
                        <template #default="{row}">
                            <el-tag :type="row.input_type == 1 ? 'info' : row.input_type == 2 ? 'primary' : row.input_type == 3 ? 'success' : 'warning'">{{row.input_type == 1 ? '采购单入库' : row.input_type == 2 ? '染色单入库' : row.input_type == 3 ? '加工单入库' : '其他入库'}}</el-tag>
                            <!-- <el-link type="primary" @click="table_show_purchase(row)">{{ row.purchase?.purchase.purchase_code }}</el-link> -->
                            <el-link :underline="false" type="primary" @click="table_show_purchase(row)">{{ row.input_type == 1 ? row.purchase?.purchase_code : row.dye?.purchase_dye_code }}</el-link>
                        </template>
                    </el-table-column>
                    <el-table-column label="入库单号" prop="apply.apply_name" width="220">
                        <template #default="{row}">
                            <div style="display: flex;align-items: center;">
                                <el-link :underline="false" type="primary" @click="table_show(row)">{{ row.purchase_ware_code }}</el-link>
                                <el-icon class="icon" color="#1e9fff" :size="20" @click="purchase_staff(row)"><el-icon-BellFilled /></el-icon>
                            </div>
                            <div>{{row.ware_date}}({{row.ware_name}})</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="供应商" prop="date" width="150">
                        <template #default="{row}">
                            <div style="margin: 10px 0;">{{row.apply.apply_name}}</div>
                            <div>{{row.apply.linkman}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="染色方" width="110">
                        <template #default="{row}">
                            <div style="margin: 10px 0;">{{row.dye_factory?row.dyeFactory.apply_name:''}}</div>
                            <div>{{row.dye_factory?row.dyeFactory.linkman:''}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="加工方" width="110">
                        <template #default="{row}">
                            <div style="margin: 10px 0;">{{row.process_factory?row.processFactory.apply_name:''}}</div>
                            <div>{{row.process_factory?row.processFactory.linkman:''}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="品名" prop="goods_name" align="center" width="120"></el-table-column>
                    <el-table-column label="数量(匹)" prop="total_num" align="center" width="120"></el-table-column>
                    <el-table-column label="可染色数量(匹)" prop="can_process_number" align="center" width="140"></el-table-column>
                    <el-table-column label="单价(元/米)" prop="price" align="center" width="120"></el-table-column>
                    <el-table-column label="审核" prop="status" width="130" align="center">
                        <template #default="{row}">
                            <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="purchase_check(row)" v-auth="'purchasehouse.check'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="文件" prop="upload" width="130" align="center">
                        <template #default="{row}">
                            <el-button type="primary" size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'purchasehouse.upload'">查看</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" prop="" align="center" width="120"></el-table-column>
                    <el-table-column label="业务员" prop="" align="center" width="120"></el-table-column>
                </scTable>
            </el-main>
        </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
	</el-dialog>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
    <purchase-dialog v-if="dialog.purchase" ref="purchaseDialog" @success="handleSuccess" @closed="dialog.purchase = false"></purchase-dialog>
    <dye-dialog v-if="dialog.dye" ref="dyeDialog" @success="handleSuccess" @closed="dialog.dye = false"></dye-dialog>
    <dyehouse-dialog v-if="dialog.dyehouse" ref="dyehouseDialog" @success="handleSuccess" @closed="dialog.dye = false"></dyehouse-dialog>
    <upload-dialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload = false"></upload-dialog>
</template>

<script>
import CheckStatus from '../../checkStatus.vue'
import uploadDialog from '../../upload.vue'
import saveDialog from './save'
import purchaseDialog from '../purchase/save'
import dyeDialog from '@/views/business/dye/order/save.vue'
import dyehouseDialog from '@/views/business/dye/house/save.vue'
export default {
    props: {
        query: { type: Object, default: () => ({}) },
    },
	emits: ['success', 'closed'],
    components: {
        saveDialog, CheckStatus, purchaseDialog, uploadDialog, dyeDialog, dyehouseDialog
    },
	data() {
		return {
			dialog: {
                save: false,
                check: false,
                purchase: false,
                upload: false,
                look: false,
            },
			mode: "add",
			titleMap: {
				add: '选择入库单',
				edit: '查看签名',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
            apiObj: this.$API.business.purchase.house.list,
            selection: [],
            search: {
                purchase_code: null,
                date: null,
                status: null,
                goods_name: null,
                input_type: 1,
            },
            type: 0,
            applyList: [],
		}
	},
	mounted() {
		this.getApply();
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
        setData(data){
            this.type = data
        },
        //本地更新数据
        handleSuccess(data, mode) {
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            } else if (mode == 'house') {
                this.$refs.table.refresh()
            }
        },
        //查看
        table_show(row) {
            this.dialog[row.input_type == 1 ? 'save' : 'dyehouse'] = true
            this.$nextTick(() => {
                this.$refs[row.input_type == 1 ? 'saveDialog' : 'dyehouseDialog'].open('show').setData(row)
            })
        },
        // 采购审核
        purchase_check(row){
            this.dialog.check = true
            this.$nextTick(() => {
                this.$refs.checkDialog.open(row.input_type == 1 ? 'house' : 'dye_house' ).setData(row)
            })
        },
        // 查看文件
        purchase_upload(row){
            this.dialog.upload = true
            this.$nextTick(() => {
                this.$refs.uploadDialog.open('show').setData(row)
            })
        },
        // 查看采购单
        table_show_purchase(row){
            let query = {}
            if(row.input_type ==1){
                query = {
                    id: row.purchase_id,
                    purchase_code: row.purchase.purchase_code,
                }
            }else {
                query = {
                    id: row.dye_id,
                    purchase_dye_code: row.dye.purchase_dye_code,
                }
            }
            this.dialog[row.input_type == 1 ? 'purchase' : 'dye'] = true
            this.$nextTick(() => {
                this.$refs[row.input_type == 1 ? 'purchaseDialog' : 'dyeDialog'].open('show').setData(query)
            })
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.search.input_type = 1;
            this.$refs.table.upData(this.search)
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        back(){
            this.$emit('success', this.selection)
            this.visible = false;
        },
        // 供应商列表
		async getApply() {
			var res = await this.$API.basic.apply.all.get();
			this.applyList = res.data
		},
	}
}
</script>

<style lang="scss">
.el-input-group__append{
    padding: 0 8px;
}
</style>
